# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

from coin.exchange.base.rest.async_private_client_base import AsyncPrivateClientBase
from coin.exchange.bitflyer_v1.kr_rest.private_client import BitflyerPrivateClient


class BitflyerAsyncPrivateClient(AsyncPrivateClientBase):
  def __init__(self, key_file, *, market_subtype, timeout=20, max_connections=10, tps_limiter=None):
    self._private_client = BitflyerPrivateClient(key_file, timeout=timeout, tps_limiter=tps_limiter)
    self._market_subtype = market_subtype
    super().__init__(self._private_client, max_connections)

  def async_query_account_balance(self):
    fut = None
    if self._market_subtype == "Spot":
      fut = self._executor.submit(self._private_client.query_account_balance_spot_only_impl)
    elif self._market_subtype == "FX":
      fut = self._executor.submit(self._private_client.query_account_balance_futures_only_impl)
    else:
      raise NotImplementedError()
    return fut

  def async_query_account_position(self):
    fut = None
    if self._market_subtype == "Spot":
      pass
    elif self._market_subtype == "FX":
      fut = self._executor.submit(self._private_client.query_account_position)
    else:
      raise NotImplementedError()
    return fut

  def async_query_list_orders(self, product):
    fut = self._executor.submit(
        self._private_client.nprivc.query_list_orders,
        product.native_symbol,
        child_order_acceptance_id=None,
        child_order_state=None,
        count=100,
        before=None,
        after=None,
    )
    return fut

  def async_query_submit(self, product, side, price, qty, minute_to_expire=1):
    fut = self._executor.submit(self._private_client.nprivc.query_submit_order,
                                product.native_symbol,
                                'LIMIT',
                                side,
                                price,
                                qty,
                                minute_to_expire=minute_to_expire,
                                time_in_force="GTC")
    return fut
